package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.cursor.RecyclerViewCursorAdapter;
import com.linkage.educloud.ah.listener.OnItemClickListener;
import com.linkage.educloud.ah.widget.ChatItemView;


/**
 * Created by 周振 on 2017/6/22.
 */

public class MessageAdapter extends RecyclerViewCursorAdapter<MessageAdapter.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private OnItemClickListener listener;
    /**
     * Recommended constructor.
     *
     * @param context The context
     * @param c       The cursor from which to get the data.
     * @param flags   Flags used to determine the behavior of the adapter;
     *                Currently it accept {@link #FLAG_REGISTER_CONTENT_OBSERVER}.
     */
    public MessageAdapter(Context context, Cursor c, int flags, OnItemClickListener listener) {
        super(context, c, flags);
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        this.listener = listener;
    }

    @Override
    protected void onContentChanged() {

    }

    @Override
    public MessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, Cursor cursor, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.fragment_message_item,parent,false),cursor);
    }

    public class ViewHolder extends RecyclerViewCursorAdapter.BaseViewHolder implements View.OnClickListener{
        ChatItemView chatView;
        public ViewHolder(View itemView , Cursor cursor) {
            super(itemView);
            chatView = (ChatItemView) itemView;
            chatView.init(cursor);
            chatView.setOnClickListener(this);
        }

        @Override
        public void setData(Cursor cursor) {
            chatView.bindView(cursor);
        }

        @Override
        public void onClick(View v) {
            listener.onItemClick(v,getAdapterPosition());
        }
    }
}
